package br.com.comanda.global;

import java.text.SimpleDateFormat;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import br.com.comanda.R;
import br.com.comanda.pojo.Comanda;

public class UltimosConsumosAdapter extends ArrayAdapter<Comanda> {

	Context context;
	int layoutResourceId;
	List<Comanda> listComanda;

	public UltimosConsumosAdapter(Context context, int layoutResourceId,
			List<Comanda> listComanda) {
		super(context, layoutResourceId, listComanda);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.listComanda = listComanda;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ComandaHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ComandaHolder();
			holder.txtLocal = (TextView) row.findViewById(R.id.txtLocal);
			holder.txtData = (TextView) row.findViewById(R.id.txtData);

			row.setTag(holder);
		} else {
			holder = (ComandaHolder) row.getTag();
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		
		Comanda comanda = listComanda.get(position);
		holder.txtLocal.setText(comanda.getLocal());
		holder.txtData.setText(dateFormat.format(comanda.getData()));
		
		return row;
	}

	static class ComandaHolder {
		TextView txtLocal;
		TextView txtData;
	}
}